package co2.bean;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.Co2Constants;
import co2.engine.DAOInterface;
import co2.pojos.Material;
import co2.pojos.MstRole;
import co2.pojos.PR;

@ManagedBean
@ViewScoped
public class PrMasterBean  extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;
	private PR pr;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private PR selObj;
	private List<MstRole> listRole;
	private String content; 
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
	private boolean svStatus;
	private boolean edStatus;
	
	
	@PostConstruct
	public void postCon(){
		pr = new PR();
		
		try{
			Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			
			if(map.get("idpr")!=null){
				String idpr = map.get("idpr").toString();
				pr = (PR) dao.get(PR.class, idpr);
				svStatus=false;
				edStatus=true;
			}else{
				svStatus=true;
				edStatus=false;
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	public void save(){
		
		try{
			
			
			String id = (String) (new UUIDHexGenerator()).generate(null, null);
			pr.setId(id);
			
			dao.save(pr);
			String extUrl=FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

			FacesContext.getCurrentInstance().getExternalContext().redirect(extUrl+"/jsf/target/list_pr.jsf");
			
		}catch(Exception e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Saving", ""));
			e.printStackTrace();
		}finally{
			pr = new PR();
		}
	}
	
	public void saveUpdate(){

		
		try{
			 
			dao.update(pr);
			String extUrl=FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

			FacesContext.getCurrentInstance().getExternalContext().redirect(extUrl+"/jsf/target/list_pr.jsf");
			
		}catch(Exception e){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Saving", ""));
			e.printStackTrace();
		}finally{
			pr=new PR();
		}
	
	}

	public PR getPr() {
		return pr;
	}

	public void setPr(PR pr) {
		this.pr = pr;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public PR getSelObj() {
		return selObj;
	}

	public void setSelObj(PR selObj) {
		this.selObj = selObj;
	}

	public List<MstRole> getListRole() {
		return listRole;
	}

	public void setListRole(List<MstRole> listRole) {
		this.listRole = listRole;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isSvStatus() {
		return svStatus;
	}

	public void setSvStatus(boolean svStatus) {
		this.svStatus = svStatus;
	}

	public boolean isEdStatus() {
		return edStatus;
	}

	public void setEdStatus(boolean edStatus) {
		this.edStatus = edStatus;
	}

	public SimpleDateFormat getSdf() {
		return sdf;
	}
	
	
}
